﻿using log4net;
using ServerControl.Dal;
using ServerControl.Model;
using ServerControl.WebManage.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ServerControl.Utils;

namespace ServerControl.WebManage.Controllers.Admin
{
    [RoutePrefix("admin/script")]
    [Auth(AllowRole = Model.Enum.Role.AdminOrUser)]
    public class ScriptController : BaseController
    {
        private ILog logger = LogManager.GetLogger("ScriptController");

        [Route("index")]
        public ActionResult Index()
        {
            var users = UserDal.Users.SkipWhile(u => u.UserName == "admin" || u.ExpireDate <= DateTime.Now).ToList();
            var originDomain = ConfigHelper.GetAppSetting("QiniuOriginDomain");
            originDomain = originDomain.EndsWith("/") ? originDomain : originDomain + "/";
            ViewBag.QiniuOriginDomain = originDomain;
            ViewBag.Users = users;

            return View();
        }

        /// <summary>
        /// 获取当前用户可用的脚本列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("list")]
        public JsonResult GetScripts()
        {
            try
            {
                var scripts = ScriptDal.GetScripts();

                return new JsonNetResult { Data = new SuccessResult<List<Script>> { Data = scripts, Msg = "" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("list-page")]
        public JsonResult ListPage(int page = 1, int limit = 20, string name = "", int scriptType = 0)
        {
            try
            {
                int records = 0;
                var scripts = ScriptDal.ListPage(ref records, page, limit, name, CurrentUser.UserName, scriptType);
                return new JsonNetResult { Data = new SuccessResult<List<Script>> { Data = scripts, Count = records, Msg = "" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("add")]
        public JsonResult Add(Script script)
        {
            try
            {
                script.UserName = string.IsNullOrEmpty(script.UserName) ? "" : script.UserName;
                script.AddTime = DateTime.Now;
                var isSuccess = ScriptDal.Add(script);

                if (isSuccess)
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
                else
                {
                    return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("edit")]
        public JsonResult Edit(Script script)
        {
            try
            {
                script.UserName = string.IsNullOrEmpty(script.UserName) ? "" : script.UserName;
                var isSuccess = ScriptDal.Edit(script);

                if (isSuccess)
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
                else
                {
                    return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("delete")]
        public JsonResult Delete(int id)
        {
            try
            {
                var isSuccess = ScriptDal.Delete(id);

                if (isSuccess)
                {
                    return new JsonNetResult { Data = new SuccessResult { Msg = "操作成功" } };
                }
                else
                {
                    return new JsonNetResult { Data = new FailResult { Msg = "操作失败" } };
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }

        [HttpPost]
        [Route("get")]
        public JsonResult Get(int id)
        {
            try
            {
                var script = ScriptDal.GetScript(id);
                return new JsonNetResult { Data = new SuccessResult<Script> { Data = script, Msg = "操作成功" } };
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }

            return new JsonNetResult { Data = new FailResult { Msg = "请求异常" } };
        }
    }
}